﻿using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Implementation;
using NetTopologySuite.IO;
using NetTopologySuite.Operation.OverlayNG;
using NetTopologySuite.Operation.Union;
using NUnit.Framework;

namespace NetTopologySuite.Samples.Tests.Github
{
    public class Issue531
    {
        [Test]
        public void Test()
        {
            const string WKT1 = "MULTIPOLYGON (((-187352.51466001172 254443.18465124597, -187352.51470705227 254443.18449617404, -187525 254354, -187727 254254, -187815 254212, -188152 254058, -188811 253801, -190078 253474, -191878 253385, -193872 253760, -196599 254930, -199247 256559, -201853 258406, -204439.97021253 260284.9783646478, -204439.97021251227 260284.9783646359, -201852.99992144763 258406.00010929993, -200025.52354523647 257110.77781586334, -200000.59812369465 257113.49672827913, -199986.67172811634 257109.01427324995, -199981.8909221748 257104.043747472, -199978.8398234392 257096.87652093743, -199978.2955040013 257084.65599078746, -199982.5582782152 257080.32622404586, -199247.02654488187 256559.01881365958, -196599.0622967351 254930.03832379912, -193872.10091189877 253760.0432956414, -191878.11575613206 253385.02176993643, -190078.0015521332 253474.0101937663, -188811.00109127734 253801.00338319998, -188152.0264580034 254057.98968178016, -187815.0075782772 254211.9965369397, -187524.9922187016 254354.00402336728, -187352.51466001172 254443.18465124597)), ((-187356.80544374793 254429.03983289757, -188050.46774594585 252142.34167635604, -189245.70509374954 249906.20987775913, -190854.2239637224 247946.2239637224, -192814.2098777591 246337.70509374957, -195050.341676356 245142.46774594585, -197476.6860685302 244406.44422129053, -199999.99999999997 244157.9194910694, -202523.31393146978 244406.44422129053, -204949.65832364396 245142.46774594582, -207185.79012224087 246337.70509374954, -209145.77603627756 247946.22396372238, -210754.29490625043 249906.2098777591, -211949.53225405415 252142.34167635598, -212685.55577870947 254568.68606853017, -212934.0805089306 257091.99999999997, -212685.55577870947 259615.31393146975, -211949.53225405418 262041.65832364393, -210951.54235461148 263908.7661017961, -209747 263472, -207050 262023, -204447 260275, -201860 258396, -199254 256549, -196604 254920, -193875 253750, -191879 253374, -190076 253464, -188807 253791, -188147 254048, -187810 254203, -187723 254245, -187521 254344, -187356.80544374793 254429.03983289757)), ((-210657.41027100087 263815.59807140817, -209742.13727021235 263484.04972474, -209742.13727021223 263484.0497247399, -210657.41027100087 263815.59807140817)))";
            const string WKT2 = "MULTIPOLYGON (((-199979.8753882025 257092, -199980.2620769752 257088.07388300396, -199981.40728306054 257084.29864448233, -199983.266996831 257080.81936473423, -199985.76975052923 257077.76975052923, -199988.81936473423 257075.266996831, -199992.29864448233 257073.40728306054, -199996.07388300396 257072.2620769752, -200000 257071.8753882025, -200003.92611699604 257072.2620769752, -200007.70135551767 257073.40728306054, -200011.18063526577 257075.266996831, -200014.23024947077 257077.76975052923, -200016.733003169 257080.81936473423, -200018.59271693946 257084.29864448233, -200019.7379230248 257088.07388300396, -200020.1246117975 257092, -200019.7379230248 257095.92611699604, -200018.59271693946 257099.70135551767, -200016.733003169 257103.18063526577, -200014.23024947077 257106.23024947077, -200011.18063526577 257108.733003169, -200007.70135551767 257110.59271693946, -200003.92611699604 257111.7379230248, -200000 257112.1246117975, -199996.07388300396 257111.7379230248, -199992.29864448233 257110.59271693946, -199988.81936473423 257108.733003169, -199985.76975052923 257106.23024947077, -199983.266996831 257103.18063526577, -199981.40728306054 257099.70135551767, -199980.2620769752 257095.92611699604, -199979.8753882025 257092)))";

            var pm = PrecisionModel.Floating.Value;
            var gs = new NtsGeometryServices(PackedCoordinateSequenceFactory.DoubleFactory, pm, 4326, GeometryOverlay.NG, new CoordinateEqualityComparer());
            var reader = new WKTReader(gs) { IsOldNtsCoordinateSyntaxAllowed = false };
            var g1 = reader.Read(WKT1);
            var g2 = reader.Read(WKT2);

            var g1g2 = g1.Union(g2);
            TestContext.WriteLine(g1g2.AsText());

            g1g2 = UnaryUnionOp.Union(new[] { g1, g2 });
            TestContext.WriteLine(g1g2.AsText());

            g1g2 = UnaryUnionNG.Union(new[] { g1, g2 }, new PrecisionModel(1E8));
            TestContext.WriteLine(g1g2.AsText());

            g1g2 = UnaryUnionNG.Union(new[] { g1, g2 }, pm);
            TestContext.WriteLine(g1g2.AsText());
        }
    }
}
